var Consultion = {
	CurrentUser : null,
	Initial : function(){
		Consultion.LoadUser();		
		var myform = new FormWatcher($(".chat_zone").find("form"),Consultion.OnSbumit);
		window.setInterval(function(){Consultion.LoadUser();Consultion.LoadData();},5000);
	},
	LoadUser : function(){
		Ajax.Request({mod:"user",op:"GetConsuler"},function(json){
			if(json.data.length){
				var htmltxt = "";
				for(var i=0;i<json.data.length;i++){
					if(i%7 == 0 && i != 0) htmltxt += "<br />";
					if(json.data[i].con_user == Consultion.CurrentUser) htmltxt += '<li><img alt="'+ json.data[i].con_user +'" src="../images/users_two_48.png" /></li>';
					else if(json.data[i].havemsg > 0) htmltxt += '<li><img alt="'+ json.data[i].con_user +'" src="../images/questionmark_48.png" /></li>';
					else htmltxt += '<li><img alt="'+ json.data[i].con_user +'" src="../images/comment_remove_48.png" /></li>';
				}
				$(".consultion_list").html(htmltxt);
				$(".consultion_list").find("img").click(function(){
					$(".chatlist").html("");
					Consultion.CurrentUser = this.alt;
					Consultion.LoadData();
					$(".chat_zone").slideDown("slow");
				});
			}
		},false);
	},
	OnSbumit : function(){
		var txt = $(".chat_zone").find("textarea").val();
		if(txt.length < 1) return;
		var param = {mod:"user",op:"AddUserChat",content:txt,user:Consultion.CurrentUser};
		Ajax.Request(param,function(json){
			if(json.data === true){
				$(".chat_zone").find("textarea").val("");
				Consultion.LoadData();
			}
		});
		return false;
	},
	LoadData : function(){
		if(Consultion.CurrentUser == null) return;		
		Ajax.Request({mod:"user",op:"GetUserChatContent",user:Consultion.CurrentUser},function(json){
			if(json.data.length){
				var tpltxt = '<li><div class="time">#{con_time}</div><div class="#{classname}">#{con_content}</div></li>';
				var template = new Template(tpltxt);
				var htmltxt = "";
				var haveReply = false;
				for(var i=0;i<json.data.length;i++){
					var d = json.data[i];
					if(d.con_from == "USER") d.classname = "content2";
					else d.classname = "content1";
					htmltxt += template.Evaluate(d);
					if(d.con_view == 0) haveReply = true;
				}
				$(".chatlist").html(htmltxt);
				$(".chat_zone").find("textarea").focus();
			}
		},false);
	},
	CloseWin : function(){
		Consultion.CurrentUser = null;
		$(".chat_zone").slideUp("slow");
	}
}
$(Consultion.Initial);